import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

public class Update extends Thread {

    private Socket socketTracker = null;
    private PrintWriter out;
    private BufferedReader in;


	public Update(Socket sock, PrintWriter pw, BufferedReader br)
	{
		super();
		this.socketTracker = sock;
		this.out = pw;
		this.in = br;
	}


    @Override
	public void run()
	{
		while(true)
		{
			try
			{
				Thread.sleep(Config.INTERVAL_MAJ*1000);
				this.sendToTracker(Tools.getStringUpdate());
				this.receiveFromTracker();
				
			}catch(InterruptedException e){}
		}
	}

	

	private void sendToTracker(String s)
	{
		if(this.socketTracker == null || !this.socketTracker.isConnected())
		{
			System.out.println("Socket not connected.");
			return;
		}

		PrintWriter send_s = this.out;
		send_s.print(s);
		send_s.flush();
		Tools.traceFluxSend(s);
	}


	private String receiveFromTracker()
	{
		if(this.socketTracker == null || !this.socketTracker.isConnected())
		{
			System.out.println("Socket not connected.");
			return null;
		}

		try{
			BufferedReader receive_s = this.in;
			String str = receive_s.readLine();
			Tools.treatResponse(str);
			Tools.traceFluxReceive(str);
			return str;
		}catch(IOException e){ return null; }
	}

}
